import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  
  const env = loadEnv(mode, process.cwd());

  return {
    plugins: [
      vue(),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    server: {
      host: env.VITE_BASE_URL,
      port: env.VITE_BASE_PORT,
      cors: true,
      proxy: {
        '/api/auth': {
          target: 'http://127.0.0.1:8084/',
          // target: 'http://192.168.100.8:32146/',
          // target: 'http://192.168.31.35:8084/',
          changeOrigin: true,
          rewrite: (path) => path.replace('/api/auth', '') // 不可以省略rewrite
        },
        '/api/store-manage': {
          target: 'http://192.168.100.8:8181/',
          changeOrigin: true,
          rewrite: (path) => path.replace('/api/store-manage', '') // 不可以省略rewrite
        },
        '/api/financial': {
          // target: 'http://192.168.100.8:32147/',
          target: 'http://192.168.100.4:8085/',
          changeOrigin: true,
          rewrite: (path) => path.replace('/api/financial/', '') // 不可以省略rewrite
        },
        '/api/dfs': {
          target: 'http://192.168.100.8:8082/',
          changeOrigin: true,
          rewrite: (path) => path.replace('/api/dfs', '') // 不可以省略rewrite
        },
        '/api/clinic': {
          target: 'http://127.0.0.1:8081/',
          // target: 'http://47.105.39.255:32150/',
          // target: 'http://192.168.100.8:32147/',
          // target: 'http://192.168.31.35:8081/',
          changeOrigin: true,
          rewrite: (path) => path.replace('/api/clinic', '') // 不可以省略rewrite
        },
        '/answer/api/v1': {
          target: 'https://forum.maliang.work/',
          changeOrigin: true,
          rewrite: (path) => path.replace('/answer/api/v1', '/answer/api/v1') // 不可以省略rewrite
        },
        '/api/application': {
          target: 'http://123.60.155.123:8080/',
          changeOrigin: true,
          rewrite: (path) => path.replace('/api/application', '/api/application') // 不可以省略rewrite
        },
      },
    },
    build: {
      rollupOptions: {
        output: {
          entryFileNames: `assets/[name].[hash].js`,
          chunkFileNames: `assets/[name].[hash].js`,
          assetFileNames: `assets/[name].[hash].[ext]`,
         
        }
      },
    }
  }
})

